package com.wgchao.authmanager.infrastructure.convert.em;

import com.wgchao.authmanager.domain.model.Operator;
import com.wgchao.authmanager.domain.model.Role;
import com.wgchao.authmanager.domain.model.RoleIdentity;
import com.wgchao.authmanager.infrastructure.convert.Convert;
import com.wgchao.authmanager.infrastructure.jpa.entity.RoleInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

/**
 * @ClassName: RoleInfoConvert
 * @author: wgchao
 * @createTime: 2020/5/7 3:12 PM
 * @Description: TODO
 */
@Component
public class RoleInfoConvert implements Convert<Role, RoleInfo> {
    @Override
    public RoleInfo toEntity(Role role) {
        RoleInfo roleInfo = new RoleInfo();
        BeanUtils.copyProperties(role, roleInfo);

        if(role.getCreator() != null){
            roleInfo.setCreateUser(role.getCreator().getUname());
            roleInfo.setCreateTime(role.getCreator().getOperateTime());
        }
        if(role.getLastupdator() != null){
            roleInfo.setLastUpdateUser(role.getLastupdator().getUname());
            roleInfo.setLastUpdateTime(role.getLastupdator().getOperateTime());
        }
        roleInfo.setId(role.findId());
        roleInfo.setVersion(role.findVersion());
        return roleInfo;
    }

    @Override
    public Role toModel(RoleInfo roleInfo) {
        return new Role(roleInfo.getId(),
                new RoleIdentity(roleInfo.getAgencyCode(), roleInfo.getRoleName()),
                roleInfo.getRemark(), roleInfo.isDisabled(),
                new Operator(roleInfo.getCreateUser(), roleInfo.getCreateTime()),
                new Operator(roleInfo.getLastUpdateUser(), roleInfo.getLastUpdateTime()), roleInfo.getVersion());
    }
}
